package com.sizu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sizu.common.dto.GoodsDto;
import com.sizu.common.statisticaldto.TStatementDto;
import com.sizu.common.utils.ExcelBean;
import com.sizu.common.utils.ExcelUtils;
import com.sizu.entity.TStatement;
import com.sizu.mapper.TStatementMapper;
import com.sizu.service.ITStatementService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵玉玺
 * @since 2020-02-29
 */
@Service
public class TStatementServiceImpl extends ServiceImpl<TStatementMapper, TStatement> implements ITStatementService {

    @Resource
    private TStatementMapper tStatementMapper;

    @Override
    public List<TStatement> getmonthsList(TStatement tStatement) {
        return tStatementMapper.getmonthsList(tStatement);
    }

    @Override
    public List<TStatement> getdayList(TStatement tStatement) {
        return tStatementMapper.getdayList(tStatement);
    }

    @Override
    public XSSFWorkbook downLoadDayExcel(TStatementDto tStatementDto) throws Exception {
        PageHelper.startPage(tStatementDto.getPageNum(),tStatementDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();
        TStatement tStatement=new TStatement();
        BeanUtils.copyProperties(tStatementDto,tStatement);
        List<TStatement> tStatements = getdayList(tStatement);
        PageInfo<TStatement> pageInfo = new PageInfo(tStatements);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("日期","dates",0));
        excelBeans.add(new ExcelBean("销售金额","salePrice",0));
        excelBeans.add(new ExcelBean("成本金额","cost",0));
        excelBeans.add(new ExcelBean("盈利金额","profit",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(TStatement.class,pageInfo.getList(),map,"按日统计");
    }

    @Override
    public XSSFWorkbook downLoadMonthExcel(TStatementDto tStatementDto) throws Exception {
        PageHelper.startPage(tStatementDto.getPageNum(),tStatementDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();
        TStatement tStatement=new TStatement();
        BeanUtils.copyProperties(tStatementDto,tStatement);
        List<TStatement> tStatements = getmonthsList(tStatement);
        PageInfo<TStatement> pageInfo = new PageInfo(tStatements);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("日期","months",0));
        excelBeans.add(new ExcelBean("销售金额","salePrice",0));
        excelBeans.add(new ExcelBean("成本金额","cost",0));
        excelBeans.add(new ExcelBean("盈利金额","profit",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(TStatement.class,pageInfo.getList(),map,"按日统计");
    }


}
